package com.united.ucts.modules.system.controller;

import com.united.frame.common.module.result.Result;
import com.united.ucts.modules.system.pojo.po.ResourceCategory;
import com.united.ucts.modules.system.service.ResourceCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 后台资源分类管理Controller
 *
 * @author Hudan
 * @version 1.0
 * @filename ResourceCategoryController.java
 * @copyright <a href="www.united.cn">www.united.cn</a>
 * @date 2019-08-16
 */
@Api(tags = "ResourceCategoryController", value = "后台资源分类管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    private final ResourceCategoryService resourceCategoryService;

    @ApiOperation("添加后台资源分类")
    @PostMapping(value = "/create")
    public Result<Integer> create(@Valid @RequestBody ResourceCategory resourceCategory) {
        int count = resourceCategoryService.create(resourceCategory);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping(value = "/delete/{id}")
    public Result<Integer> delete(@PathVariable String id) {
        int count = resourceCategoryService.delete(id);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("修改后台资源分类")
    @PostMapping(value = "/update/{id}")
    public Result<Integer> update(@PathVariable String id, @Valid @RequestBody ResourceCategory resourceCategory) {
        int count = resourceCategoryService.update(id, resourceCategory);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("查询所有后台资源分类")
    @GetMapping(value = "/listAll")
    public Result<List<ResourceCategory>> listAll() {
        List<ResourceCategory> resourceList = resourceCategoryService.listAll();
        return Result.success(resourceList);
    }
}